package followme.yaohuile.com.followme.parsejson.recommend.community;

import android.os.Handler;
import android.os.Message;

import com.alibaba.fastjson.JSON;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;

import org.json.JSONException;
import org.json.JSONObject;

import followme.yaohuile.com.followme.bean.recommend.community.CommunityData;
import followme.yaohuile.com.followme.network.recommend.NetWork;
import followme.yaohuile.com.followme.utils.LogUtils;


/**
 * Created by Lenovo on 2015/11/13.
 */
public class CommunityDataParamers {
//    public static final int COUNTS = 1;
//    public static final int FOURM_LIST = 2;
//    public static final int GROUP_ITEM = 3;
//    public static final int TYPE_ITEM = 4;


    //    public static final String TAG = "community";
    public static void getCommunityList(final String url, final Handler handler) {
        System.out.println("url = " + url);
        NetWork.getCommunityList(url, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                LogUtils.LOG_D(getClass(), "result==" + responseInfo.result);
                //        获取数据成功，开始解析
                try {

                    JSONObject jsonObject = new JSONObject(responseInfo.result);
                    JSONObject jsondata = jsonObject.getJSONObject("data");
                    CommunityData data = JSON.parseObject(jsondata.toString(), CommunityData.class);
                    LogUtils.LOG_D(getClass(), "data==" + data.toString());
                    Message community_message = new Message();
                    community_message.what = 1;
                    community_message.obj = data;
                    handler.sendMessage(community_message);
//                    JSONObject data = jsonObject.getJSONObject("data");
//
//                    JSONObject counts = data.getJSONObject("counts");
//                    CommunityCounts communityCounts = JSON.parseObject(counts.toString(), CommunityCounts.class);
//                    LogUtils.LOG_D(getClass(), "counts==========" + communityCounts.toString());
////
////                    通过handle传递到CommunityFragment中使用
////                    1.传递CommunityCounts的对象
//                    Message counts_message = new Message();
//                    counts_message.what = 1;
//                    counts_message.obj = communityCounts;
//                    handler.sendMessage(counts_message);
//
////                    解析forum_list
//                    JSONArray forum_list = data.getJSONArray("forum_list");
//                    for (int i = 0; i < forum_list.length(); i++) {
//                        JSONObject forum_list_item = (JSONObject) forum_list.get(i);
////                        解析到forum_list中的内容
//                        CommunityForum_list communityForum_list = JSON.parseObject(forum_list_item.toString(), CommunityForum_list.class);
//                        LogUtils.LOG_D(getClass(), "communityForum_list==" + communityForum_list.toString());
////                        解析到标题
////                        JSONObject name = forum_list_item.getJSONObject("name");
////                      将CommunityForum_list对象传到CommunityFragment中
//                        Message forum_list_message = new Message();
//                        forum_list_message.what = FOURM_LIST;
//                        forum_list_message.obj = communityForum_list;
//                        handler.sendMessage(forum_list_message);
//
////                        解析forum_list中的group
//                        JSONArray groups = forum_list_item.getJSONArray("group");
//                        for (int j = 0; j < groups.length(); j++) {
//                            JSONObject group_item = (JSONObject) groups.get(j);
////                            解析到group中的内容
//                            CommunityForum_listGroups communityForum_listGroups = JSON.parseObject(group_item.toString(), CommunityForum_listGroups.class);
//                            LogUtils.LOG_D(getClass(), "communityForum_listGroups==" + communityForum_listGroups.toString());
////  将CommunityForum_listGroups对象传到CommunityFragment中
//                            Message group_item_message = new Message();
//                            group_item_message.what = GROUP_ITEM;
//                            group_item_message.obj = communityForum_listGroups;
//                            handler.sendMessage(group_item_message);
//
//
////                            解析group_item中的types
//                            JSONArray types = group_item.getJSONArray("types");
//                            for (int v = 0; v < types.length(); v++) {
//                                JSONObject type_item = (JSONObject) types.get(v);
////                                解析types中的内容
//                                CommunityGroupTypes communityGroupTypes = JSON.parseObject(type_item.toString(), CommunityGroupTypes.class);
//                                LogUtils.LOG_D(getClass(), "communityGroupTypes" + communityGroupTypes.toString());
////                                将CommunityGroupTypes对象传到CommunityFragment中
//                                Message group_types_message = new Message();
//                                group_types_message.what = TYPE_ITEM;
//                                group_types_message.obj = communityGroupTypes;
//                                handler.sendMessage(group_types_message);
//                            }
//
//                        }
//                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }


            @Override
            public void onFailure(HttpException e, String s) {
                LogUtils.LOG_D(getClass(), "数据加载失败!");
            }
        });


    }
}
